package org.hadoop.zk;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
public class NodeWatch {
    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper("server101:2181", 30000, (e)->{
            if(e.getState()== Watcher.Event.KeeperState.SyncConnected){
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        //添加一下观察者，只有观察一次，如果希望循环观察，可以在接收到观察的信息以后再次添加观察者
        //以下将观察/test节点的数据变化
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getType()+","+event.getState());
            }
        };
        zooKeeper.getData("/test", watcher,new Stat());
        Thread.sleep(Integer.MAX_VALUE);
        zooKeeper.close();
    }
}
